package o;

import android.annotation.TargetApi;
import android.os.Build.VERSION;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.os.StrictMode.ThreadPolicy.Builder;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class rm implements Closeable {
    private long ʻ;
    private long ʼ = 0;
    private final int ʽ;
    private final File ˊ;
    private int ˊॱ;
    private final File ˋ;
    final ThreadPoolExecutor ˎ = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new e());
    private final File ˏ;
    private final Callable<Void> ˏॱ = new Callable<Void>(this) {
        final /* synthetic */ rm ॱ;

        {
            this.ॱ = r1;
        }

        public /* synthetic */ Object call() throws Exception {
            return ˊ();
        }

        public Void ˊ() throws Exception {
            synchronized (this.ॱ) {
                if (this.ॱ.ᐝ == null) {
                } else {
                    this.ॱ.ʼ();
                    if (this.ॱ.ˊ()) {
                        this.ॱ.ˋ();
                        this.ॱ.ˊॱ = 0;
                    }
                }
            }
            return null;
        }
    };
    private long ͺ = 0;
    private final File ॱ;
    private final LinkedHashMap<String, a> ॱˊ = new LinkedHashMap(0, 0.75f, true);
    private final int ॱॱ;
    private Writer ᐝ;

    final class a {
        private boolean ʻ;
        private long ʼ;
        final /* synthetic */ rm ˊ;
        File[] ˋ;
        private final String ˎ;
        private final long[] ˏ;
        File[] ॱ;
        private b ॱॱ;

        private a(rm rmVar, String str) {
            this.ˊ = rmVar;
            this.ˎ = str;
            this.ˏ = new long[rmVar.ʽ];
            this.ˋ = new File[rmVar.ʽ];
            this.ॱ = new File[rmVar.ʽ];
            StringBuilder append = new StringBuilder(str).append('.');
            int length = append.length();
            for (int i = 0; i < rmVar.ʽ; i++) {
                append.append(i);
                this.ˋ[i] = new File(rmVar.ˋ, append.toString());
                append.append(".tmp");
                this.ॱ[i] = new File(rmVar.ˋ, append.toString());
                append.setLength(length);
            }
        }

        public String ˋ() throws IOException {
            StringBuilder stringBuilder = new StringBuilder();
            for (long append : this.ˏ) {
                stringBuilder.append(' ').append(append);
            }
            return stringBuilder.toString();
        }

        private void ˊ(String[] strArr) throws IOException {
            if (strArr.length != this.ˊ.ʽ) {
                throw ॱ(strArr);
            }
            int i = 0;
            while (i < strArr.length) {
                try {
                    this.ˏ[i] = Long.parseLong(strArr[i]);
                    i++;
                } catch (NumberFormatException e) {
                    throw ॱ(strArr);
                }
            }
        }

        private IOException ॱ(String[] strArr) throws IOException {
            throw new IOException("unexpected journal line: " + Arrays.toString(strArr));
        }

        public File ˏ(int i) {
            return this.ˋ[i];
        }

        public File ˋ(int i) {
            return this.ॱ[i];
        }
    }

    public final class b {
        private final a ˊ;
        private boolean ˋ;
        final /* synthetic */ rm ˏ;
        private final boolean[] ॱ;

        private b(rm rmVar, a aVar) {
            this.ˏ = rmVar;
            this.ˊ = aVar;
            this.ॱ = aVar.ʻ ? null : new boolean[rmVar.ʽ];
        }

        public File ॱ(int i) throws IOException {
            File ˋ;
            synchronized (this.ˏ) {
                if (this.ˊ.ॱॱ != this) {
                    throw new IllegalStateException();
                }
                if (!this.ˊ.ʻ) {
                    this.ॱ[i] = true;
                }
                ˋ = this.ˊ.ˋ(i);
                if (!this.ˏ.ˋ.exists()) {
                    this.ˏ.ˋ.mkdirs();
                }
            }
            return ˋ;
        }

        public void ˎ() throws IOException {
            this.ˏ.ॱ(this, true);
            this.ˋ = true;
        }

        public void ˋ() throws IOException {
            this.ˏ.ॱ(this, false);
        }

        public void ॱ() {
            if (!this.ˋ) {
                try {
                    ˋ();
                } catch (IOException e) {
                }
            }
        }
    }

    public final class c {
        private final File[] ˊ;
        private final String ˋ;
        private final long ˎ;
        final /* synthetic */ rm ˏ;
        private final long[] ॱ;

        private c(rm rmVar, String str, long j, File[] fileArr, long[] jArr) {
            this.ˏ = rmVar;
            this.ˋ = str;
            this.ˎ = j;
            this.ˊ = fileArr;
            this.ॱ = jArr;
        }

        public File ˋ(int i) {
            return this.ˊ[i];
        }
    }

    static final class e implements ThreadFactory {
        private e() {
        }

        public synchronized Thread newThread(Runnable runnable) {
            Thread thread;
            thread = new Thread(runnable, "glide-disk-lru-cache-thread");
            thread.setPriority(1);
            return thread;
        }
    }

    private rm(File file, int i, int i2, long j) {
        this.ˋ = file;
        this.ॱॱ = i;
        this.ॱ = new File(file, "journal");
        this.ˏ = new File(file, "journal.tmp");
        this.ˊ = new File(file, "journal.bkp");
        this.ʽ = i2;
        this.ʻ = j;
    }

    public static rm ॱ(File file, int i, int i2, long j) throws IOException {
        if (j <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        } else if (i2 <= 0) {
            throw new IllegalArgumentException("valueCount <= 0");
        } else {
            File file2 = new File(file, "journal.bkp");
            if (file2.exists()) {
                File file3 = new File(file, "journal");
                if (file3.exists()) {
                    file2.delete();
                } else {
                    ˊ(file2, file3, false);
                }
            }
            rm rmVar = new rm(file, i, i2, j);
            if (rmVar.ॱ.exists()) {
                try {
                    rmVar.ˎ();
                    rmVar.ˏ();
                    return rmVar;
                } catch (IOException e) {
                    System.out.println("DiskLruCache " + file + " is corrupt: " + e.getMessage() + ", removing");
                    rmVar.ॱ();
                }
            }
            file.mkdirs();
            rmVar = new rm(file, i, i2, j);
            rmVar.ˋ();
            return rmVar;
        }
    }

    private void ˎ() throws IOException {
        Closeable rnVar = new rn(new FileInputStream(this.ॱ), rs.ॱ);
        int i;
        try {
            String ॱ = rnVar.ॱ();
            String ॱ2 = rnVar.ॱ();
            String ॱ3 = rnVar.ॱ();
            String ॱ4 = rnVar.ॱ();
            String ॱ5 = rnVar.ॱ();
            if ("libcore.io.DiskLruCache".equals(ॱ) && "1".equals(ॱ2) && Integer.toString(this.ॱॱ).equals(ॱ3) && Integer.toString(this.ʽ).equals(ॱ4) && "".equals(ॱ5)) {
                i = 0;
                while (true) {
                    ˋ(rnVar.ॱ());
                    i++;
                }
            } else {
                throw new IOException("unexpected journal header: [" + ॱ + ", " + ॱ2 + ", " + ॱ4 + ", " + ॱ5 + "]");
            }
        } catch (EOFException e) {
            this.ˊॱ = i - this.ॱˊ.size();
            if (rnVar.ˊ()) {
                ˋ();
            } else {
                this.ᐝ = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.ॱ, true), rs.ॱ));
            }
            rs.ॱ(rnVar);
        } catch (Throwable th) {
            rs.ॱ(rnVar);
        }
    }

    private void ˋ(String str) throws IOException {
        int indexOf = str.indexOf(32);
        if (indexOf == -1) {
            throw new IOException("unexpected journal line: " + str);
        }
        String str2;
        int i = indexOf + 1;
        int indexOf2 = str.indexOf(32, i);
        if (indexOf2 == -1) {
            String substring = str.substring(i);
            if (indexOf == "REMOVE".length() && str.startsWith("REMOVE")) {
                this.ॱˊ.remove(substring);
                return;
            }
            str2 = substring;
        } else {
            str2 = str.substring(i, indexOf2);
        }
        a aVar = (a) this.ॱˊ.get(str2);
        if (aVar == null) {
            aVar = new a(str2);
            this.ॱˊ.put(str2, aVar);
        }
        if (indexOf2 != -1 && indexOf == "CLEAN".length() && str.startsWith("CLEAN")) {
            String[] split = str.substring(indexOf2 + 1).split(HwAccountConstants.BLANK);
            aVar.ʻ = true;
            aVar.ॱॱ = null;
            aVar.ˊ(split);
        } else if (indexOf2 == -1 && indexOf == "DIRTY".length() && str.startsWith("DIRTY")) {
            aVar.ॱॱ = new b(aVar);
        } else if (indexOf2 != -1 || indexOf != "READ".length() || !str.startsWith("READ")) {
            throw new IOException("unexpected journal line: " + str);
        }
    }

    private void ˏ() throws IOException {
        ˊ(this.ˏ);
        Iterator it = this.ॱˊ.values().iterator();
        while (it.hasNext()) {
            a aVar = (a) it.next();
            int i;
            if (aVar.ॱॱ == null) {
                for (i = 0; i < this.ʽ; i++) {
                    this.ʼ += aVar.ˏ[i];
                }
            } else {
                aVar.ॱॱ = null;
                for (i = 0; i < this.ʽ; i++) {
                    ˊ(aVar.ˏ(i));
                    ˊ(aVar.ˋ(i));
                }
                it.remove();
            }
        }
    }

    private synchronized void ˋ() throws IOException {
        if (this.ᐝ != null) {
            ˊ(this.ᐝ);
        }
        Writer bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.ˏ), rs.ॱ));
        try {
            bufferedWriter.write("libcore.io.DiskLruCache");
            bufferedWriter.write("\n");
            bufferedWriter.write("1");
            bufferedWriter.write("\n");
            bufferedWriter.write(Integer.toString(this.ॱॱ));
            bufferedWriter.write("\n");
            bufferedWriter.write(Integer.toString(this.ʽ));
            bufferedWriter.write("\n");
            bufferedWriter.write("\n");
            for (a aVar : this.ॱˊ.values()) {
                if (aVar.ॱॱ != null) {
                    bufferedWriter.write("DIRTY " + aVar.ˎ + '\n');
                } else {
                    bufferedWriter.write("CLEAN " + aVar.ˎ + aVar.ˋ() + '\n');
                }
            }
            ˊ(bufferedWriter);
            if (this.ॱ.exists()) {
                ˊ(this.ॱ, this.ˊ, true);
            }
            ˊ(this.ˏ, this.ॱ, false);
            this.ˊ.delete();
            this.ᐝ = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.ॱ, true), rs.ॱ));
        } catch (Throwable th) {
            ˊ(bufferedWriter);
        }
    }

    private static void ˊ(File file) throws IOException {
        if (file.exists() && !file.delete()) {
            throw new IOException();
        }
    }

    private static void ˊ(File file, File file2, boolean z) throws IOException {
        if (z) {
            ˊ(file2);
        }
        if (!file.renameTo(file2)) {
            throw new IOException();
        }
    }

    public synchronized c ˎ(String str) throws IOException {
        c cVar = null;
        synchronized (this) {
            ʻ();
            a aVar = (a) this.ॱˊ.get(str);
            if (aVar != null) {
                if (aVar.ʻ) {
                    for (File exists : aVar.ˋ) {
                        if (!exists.exists()) {
                            break;
                        }
                    }
                    this.ˊॱ++;
                    this.ᐝ.append("READ");
                    this.ᐝ.append(' ');
                    this.ᐝ.append(str);
                    this.ᐝ.append('\n');
                    if (ˊ()) {
                        this.ˎ.submit(this.ˏॱ);
                    }
                    cVar = new c(str, aVar.ʼ, aVar.ˋ, aVar.ˏ);
                }
            }
        }
        return cVar;
    }

    public b ˏ(String str) throws IOException {
        return ˋ(str, -1);
    }

    private synchronized b ˋ(String str, long j) throws IOException {
        b bVar;
        ʻ();
        a aVar = (a) this.ॱˊ.get(str);
        if (j == -1 || (aVar != null && aVar.ʼ == j)) {
            a aVar2;
            if (aVar == null) {
                aVar = new a(str);
                this.ॱˊ.put(str, aVar);
                aVar2 = aVar;
            } else if (aVar.ॱॱ != null) {
                bVar = null;
            } else {
                aVar2 = aVar;
            }
            bVar = new b(aVar2);
            aVar2.ॱॱ = bVar;
            this.ᐝ.append("DIRTY");
            this.ᐝ.append(' ');
            this.ᐝ.append(str);
            this.ᐝ.append('\n');
            ˋ(this.ᐝ);
        } else {
            bVar = null;
        }
        return bVar;
    }

    private synchronized void ॱ(b bVar, boolean z) throws IOException {
        int i = 0;
        synchronized (this) {
            a ˏ = bVar.ˊ;
            if (ˏ.ॱॱ != bVar) {
                throw new IllegalStateException();
            }
            if (z) {
                if (!ˏ.ʻ) {
                    int i2 = 0;
                    while (i2 < this.ʽ) {
                        if (!bVar.ॱ[i2]) {
                            bVar.ˋ();
                            throw new IllegalStateException("Newly created entry didn't create value for index " + i2);
                        } else if (!ˏ.ˋ(i2).exists()) {
                            bVar.ˋ();
                            break;
                        } else {
                            i2++;
                        }
                    }
                }
            }
            while (i < this.ʽ) {
                File ˋ = ˏ.ˋ(i);
                if (!z) {
                    ˊ(ˋ);
                } else if (ˋ.exists()) {
                    File ˏ2 = ˏ.ˏ(i);
                    ˋ.renameTo(ˏ2);
                    long j = ˏ.ˏ[i];
                    long length = ˏ2.length();
                    ˏ.ˏ[i] = length;
                    this.ʼ = (this.ʼ - j) + length;
                }
                i++;
            }
            this.ˊॱ++;
            ˏ.ॱॱ = null;
            if ((ˏ.ʻ | z) != 0) {
                ˏ.ʻ = true;
                this.ᐝ.append("CLEAN");
                this.ᐝ.append(' ');
                this.ᐝ.append(ˏ.ˎ);
                this.ᐝ.append(ˏ.ˋ());
                this.ᐝ.append('\n');
                if (z) {
                    long j2 = this.ͺ;
                    this.ͺ = 1 + j2;
                    ˏ.ʼ = j2;
                }
            } else {
                this.ॱˊ.remove(ˏ.ˎ);
                this.ᐝ.append("REMOVE");
                this.ᐝ.append(' ');
                this.ᐝ.append(ˏ.ˎ);
                this.ᐝ.append('\n');
            }
            ˋ(this.ᐝ);
            if (this.ʼ > this.ʻ || ˊ()) {
                this.ˎ.submit(this.ˏॱ);
            }
        }
    }

    private boolean ˊ() {
        return this.ˊॱ >= 2000 && this.ˊॱ >= this.ॱˊ.size();
    }

    public synchronized boolean ˊ(String str) throws IOException {
        boolean z;
        int i = 0;
        synchronized (this) {
            ʻ();
            a aVar = (a) this.ॱˊ.get(str);
            if (aVar == null || aVar.ॱॱ != null) {
                z = false;
            } else {
                while (i < this.ʽ) {
                    File ˏ = aVar.ˏ(i);
                    if (!ˏ.exists() || ˏ.delete()) {
                        this.ʼ -= aVar.ˏ[i];
                        aVar.ˏ[i] = 0;
                        i++;
                    } else {
                        throw new IOException("failed to delete " + ˏ);
                    }
                }
                this.ˊॱ++;
                this.ᐝ.append("REMOVE");
                this.ᐝ.append(' ');
                this.ᐝ.append(str);
                this.ᐝ.append('\n');
                this.ॱˊ.remove(str);
                if (ˊ()) {
                    this.ˎ.submit(this.ˏॱ);
                }
                z = true;
            }
        }
        return z;
    }

    private void ʻ() {
        if (this.ᐝ == null) {
            throw new IllegalStateException("cache is closed");
        }
    }

    public synchronized void close() throws IOException {
        if (this.ᐝ != null) {
            Iterator it = new ArrayList(this.ॱˊ.values()).iterator();
            while (it.hasNext()) {
                a aVar = (a) it.next();
                if (aVar.ॱॱ != null) {
                    aVar.ॱॱ.ˋ();
                }
            }
            ʼ();
            ˊ(this.ᐝ);
            this.ᐝ = null;
        }
    }

    private void ʼ() throws IOException {
        while (this.ʼ > this.ʻ) {
            ˊ((String) ((Entry) this.ॱˊ.entrySet().iterator().next()).getKey());
        }
    }

    public void ॱ() throws IOException {
        close();
        rs.ˎ(this.ˋ);
    }

    @TargetApi(26)
    private static void ˊ(Writer writer) throws IOException {
        if (VERSION.SDK_INT < 26) {
            writer.close();
            return;
        }
        ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            writer.close();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

    @TargetApi(26)
    private static void ˋ(Writer writer) throws IOException {
        if (VERSION.SDK_INT < 26) {
            writer.flush();
            return;
        }
        ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            writer.flush();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }
}
